package cn.edu.hqu.cst.android.ljc.sqlitetest;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MySqliteOpenHelper extends SQLiteOpenHelper {

    //对外提供函数(单例模式)
    private static SQLiteOpenHelper myInstance;
    public static synchronized SQLiteOpenHelper getMyInstance(Context context){
        if (myInstance == null){                            //数据库名称               版本号--用于数据库升级
            myInstance = new MySqliteOpenHelper(context,"myDB.db",null,1);
        }
        return myInstance;
    }

    private MySqliteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    //数据库初始化时用的 -- 创建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建students表 _id name
        String sql = "create table students(_id integer primary key autoincrement, name text)";
        db.execSQL(sql);
    }

    //数据库升级用的
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}